// pages/selectSize/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    beforeData: {},
    selectTab: 'tab1',
    isShowBigImg: false,
    // 弹窗是否可见
    dialogVisitable: false,
    // 列表弹窗是否可见
    listDialogViaible: false,
    // 弹窗key
    dialogKey: '',
    // 弹窗value
    dialogValue: '',
    dialogPlaceholder:'',
    // 弹窗key
    listDialogKey: '',
    // 弹窗value
    listDialogValue: '',
    // 弹窗数据
    listDialogData: [],
    // 型号数据
    modelData: [],
    heightData: [],
    weightData: [],
    errorMsg: '',
    // 下面是要上传的数据
    model: '',
    // 是否特殊体型(true:特殊体型|false:不是特殊体型)
    special: false,
    // 身高
    height: '',
    // 体重
    weight: '',
    // 衣长
    clothesLength: '',
    // 裤长
    outsideLength: '',
    // 胸围
    chestCircumference: '',
    // 腰围
    waistWidth: '',
    // 臀围
    hipWidth: '',
    // 测量方式 1：成衣尺寸 2 净尺寸
    measure: 1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let modelData = [];
    for (let index = 11; index <= 19; index++) {
      modelData.push(index + '0')
    }
    this.setData({
      modelData: modelData
    })
    let heightData = [];
    for (let index = 110; index <= 210; index++) {
      heightData.push(index + 'cm')
    }
    this.setData({
      heightData: heightData
    })

    let weightData = [];
    for (let index = 16; index <= 130; index++) {
      weightData.push(index + 'kg')
    }
    this.setData({
      weightData: weightData
    })

    const eventChannel = this.getOpenerEventChannel()
    if (Object.keys(eventChannel).length > 0)
      eventChannel.on('sendData', (data) => {
        this.setData({
          beforeData: data
        })
      })
  },
  handelTabClick(event) {
    let key = event.currentTarget.dataset.key
    this.setData({
      selectTab: key,
      special: key == 'tab2'
    })
  },
  handelImgClick() {
    this.setData({
      isShowBigImg: !this.data.isShowBigImg
    })
  },
  handelInputClick(event) {
    this.setData({
      dialogKey: event.currentTarget.dataset.key,
      dialogValue: this.data[event.currentTarget.dataset.key],
      dialogVisitable: true,
      dialogPlaceholder: event.currentTarget.dataset.placeholder,
    })
  },
  handelSelectClick(event) {
    let key = event.currentTarget.dataset.key;
    let listData = [];
    let listDialogValue = '';
    switch (key) {
      case 'model':
        listData = this.data.modelData;
        listDialogValue = this.data.model;
        break;
      case 'height':
        listData = this.data.heightData;
        listDialogValue = this.data.height;
        break;
      case 'weight':
        listData = this.data.weightData;
        listDialogValue = this.data.weight;
        break;
    }
    this.setData({
      listDialogKey: key,
      listDialogViaible: true,
      listDialogData: listData,
      listDialogValue
    })
  },
  onListDialogClose() {
    this.setData({
      listDialogViaible: false,
      listDialogValue: ''
    })
  },
  onListDialogSubmit(e) {
    let data = {
      listDialogViaible: false,
      listDialogValue: ''
    }
    data[e.detail.key] = e.detail.value;
    this.setData(data)
  },
  handelNextPageBtnClick() {
    let data = {
      ...this.data.beforeData,
      special: this.data.special
    };
    let msg = '';
    if (this.data.selectTab == 'tab1') {
      if (!this.data.model) {
        msg = '请选择校服型号';
      }
      data['model'] = this.data.model;
    } else {
      if (!this.data.height) {
        msg = '请选择身高';
      } else if (!this.data.weight) {
        msg = '请选择您的体重';
      } else if (!this.data.clothesLength) {
        msg = '请输入您的衣长';
      } else if (!this.data.outsideLength) {
        msg = '请输入您的裤长';
      } else if (!this.data.chestCircumference) {
        msg = '请输入您的胸围';
      } else if (!this.data.waistWidth) {
        msg = '请输入您的腰围';
      } else if (!this.data.hipWidth) {
        msg = '请输入您的臀围';
      }
      data['height'] = this.data.height;
      data['weight'] = this.data.weight;
      data['clothesLength'] = this.data.clothesLength;
      data['outsideLength'] = this.data.outsideLength;
      data['chestCircumference'] = this.data.chestCircumference;
      data['waistWidth'] = this.data.waistWidth;
      data['hipWidth'] = this.data.hipWidth;
      data['measure'] = this.data.measure;
    }

    if (msg) {
      this.setData({
        errorMsg: msg
      });
      return;
    }

    wx.navigateTo({
      url: '../confirm/index',
      success: (res) => {
        res.eventChannel.emit('sendData', data)
      }
    })
  },
  onDialogClose() {
    this.setData({
      dialogVisitable: false
    })
  },
  onDialogSubmit(e) {
    let data = {
      dialogVisitable: false,
      dialogValue: ''
    }
    data[e.detail.key] = e.detail.value;
    this.setData(data)
  },
  handelMeasureBtnClick(event) {
    this.setData({
      measure: event.currentTarget.dataset.key
    })
  },
  closeMsgDialog() {
    this.setData({
      errorMsg: ''
    });
  }
})